package com.whfc.ms.api.hdc.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.hdc.dto.AlgDeviceStreamConfigDTO;
import com.whfc.hdc.dto.AlgDeviceStreamDTO;
import com.whfc.hdc.params.AlgDeviceStreamConfigAddParam;
import com.whfc.hdc.params.AlgDeviceStreamConfigEditParam;
import com.whfc.hdc.params.AlgDeviceStreamParam;
import com.whfc.ms.api.hdc.service.MsAlgDeviceStreamService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-08-02 18:13
 */
@RequiresPermissions("hdc/alg")
@RestController
@RequestMapping("/ms/api/hdc/alg/device/stream")
public class AlgDeviceStreamController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsAlgDeviceStreamService deviceStreamService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam(value = "sn") String sn) {
        logger.info("获取算法盒子视频流地址列表, loginer:{}, sn:{}", user.getUsername(), sn);
        List<AlgDeviceStreamDTO> list = deviceStreamService.list(sn);
        return ResultUtil.success(new ListData<>(list));
    }

    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody AlgDeviceStreamParam param) {
        logger.info("新增算法盒子视频流地址, loginer:{}, param:{}", user.getUsername(), param);
        deviceStreamService.add(param);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody AlgDeviceStreamParam param) {
        logger.info("编辑算法盒子视频流地址, loginer:{}, param:{}", user.getUsername(), param);
        deviceStreamService.edit(param);
        return ResultUtil.success();
    }

    @PostMapping("/del/{streamId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable(value = "streamId") Integer streamId) {
        logger.info("删除算法盒子视频流地址, loginer:{}, streamId:{}", user.getUsername(), streamId);
        deviceStreamService.del(streamId);
        return ResultUtil.success();
    }


    @GetMapping("/config/list")
    public Result configList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam(value = "streamId") Integer streamId) {
        logger.info("获取算法盒子视频流算法配置列表, loginer:{}, streamId:{}", user.getUsername(), streamId);
        List<AlgDeviceStreamConfigDTO> list = deviceStreamService.configList(streamId);
        return ResultUtil.success(new ListData<>(list));
    }

    @PostMapping("/config/add")
    public Result configAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestBody AlgDeviceStreamConfigAddParam param) {
        logger.info("新增算法盒子视频流算法配置, loginer:{}, param:{}", user.getUsername(), param);
        deviceStreamService.configAdd(param);
        return ResultUtil.success();
    }

    @PostMapping("/config/edit")
    public Result configEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestBody AlgDeviceStreamConfigEditParam param) {
        logger.info("编辑算法盒子视频流算法配置, loginer:{}, param:{}", user.getUsername(), param);
        deviceStreamService.configEdit(param);
        return ResultUtil.success();
    }

    @PostMapping("/config/del/{id}")
    public Result configDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @PathVariable(value = "id") Integer id) {
        logger.info("删除算法盒子视频流算法配置, loginer:{}, id:{}", user.getUsername(), id);
        deviceStreamService.configDel(id);
        return ResultUtil.success();
    }


}
